<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		phuong101899
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Users extends REST_Controller
{
    function __construct(){
        parent::__construct();
        $this->load->model('users/Users_model');
        
    }
	function user_get()
    {
        // $user = $this->some_model->getSomething( $this->get('id') );
    	$users = array(
			1 => array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com', 'fact' => 'Loves swimming'),
			2 => array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com', 'fact' => 'Has a huge face'),
			3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
		);

        $this->response($users, 200); // 200 being the HTTP response code
    }
    
    function user_post()
    {
        $data = array(
            'username' => $this->post('username'),
            'password' => $this->post('password'),
            'email'    => $this->post('email')
        );
        $flag = $this->Users_model->add($data);
        $this->response($flag,200);
    }
    
    function user_delete()
    {
    	//$this->some_model->deletesomething( $this->get('id') );
        $message = array('id' => $this->get('id'), 'message' => 'DELETED!');
        
        $this->response($message, 200); // 200 being the HTTP response code
    }
    function login_post(){
        $data = array(
            'username' => $this->post('username'),
            'password' => $this->post('password'),
        );
        $save = $this->post('save');
        $flag = $this->Users_model->get_only($data);
        if($flag && is_array($flag) && !empty($flag)){
            $_SESSION['user'] = $flag;
            $this->response(array('status'=>true),200);
        }else{
            $this->response(array('status'=>false),200);
        }
    }
    function check_auth_get(){
        if(isset($_SESSION['user'])){
            $this->response(array('status'=>true,'data'=>$_SESSION['user']),200);
        }
        $this->response(array('status'=>false),200);
    }
    function logout_get(){
        unset($_COOKIE['user']);
        unset($_SESSION['user']);
        $this->response(array('status'=>true),200);
    }
}